package base;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;

public class MainFrame extends JFrame {
    private JPanel center;
    private ButtonGroup buttonGroup;

    public MainFrame(){
        setSize(new Dimension(600,400));
        setLocationRelativeTo(null);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                try {
                    Connector.getConn().close();
                } catch (SQLException e1) {
                }
                System.exit(0);
            }
        });
        setLayout(new BorderLayout());
        JPanel top = new JPanel();
        top.setBorder(BorderFactory.createLineBorder(Color.GRAY,1));
        top.setLayout(new FlowLayout(FlowLayout.LEFT,10,0));
        getContentPane().add(top,BorderLayout.NORTH);
        JLabel label = new JLabel("重新连接");
        label.setFont(new Font("微软雅黑",Font.PLAIN,18));
        top.add(label);
        JLabel label1 = new JLabel("  配置");
        label1.setFont(new Font("微软雅黑",Font.PLAIN,18));
        top.add(label1);
        label1.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                new Configuration().setVisible(true);
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                label1.setForeground(Color.blue);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                label1.setForeground(Color.black);
            }
        });
        MainFrame mainFrame = this;
        label.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                Connector.showFrame(mainFrame);
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                label.setForeground(Color.blue);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                label.setForeground(Color.black);
            }
        });
        JPanel bottom = new JPanel();
        getContentPane().add(bottom,BorderLayout.SOUTH);
        bottom.setLayout(new FlowLayout(FlowLayout.RIGHT,10,10));
        JButton jButton = new JButton("确定");
        jButton.setFont(new Font("微软雅黑",Font.PLAIN,15));
        jButton.setFocusPainted(false);
        bottom.add(jButton);
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Enumeration<AbstractButton> elements = buttonGroup.getElements();
                while (elements.hasMoreElements()){
                    AbstractButton abstractButton = elements.nextElement();
                    if (abstractButton.isSelected()) {
                        new TableFrame(abstractButton.getText()).setVisible(true);
                        break;
                    }
                }
            }
        });
        center = new JPanel();
        getContentPane().add(center,BorderLayout.CENTER);
        center.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
        showDatabases();
    }

    private void showDatabases(){
        Connection conn = Connector.getConn();
        try{
            JLabel label = new JLabel("请选择数据库(显示不全可放大窗口)：");
            label.setFont(new Font("微软雅黑",Font.PLAIN,15));
            center.add(label);
            DatabaseMetaData metaData = conn.getMetaData();
            ResultSet catalogs = metaData.getCatalogs();
            buttonGroup = new ButtonGroup();
            while (catalogs.next()){
                JRadioButton jRadioButton = new JRadioButton(catalogs.getString("TABLE_CAT"));
                jRadioButton.setFocusPainted(false);
                jRadioButton.setFont(new Font("微软雅黑",Font.PLAIN,15));
                jRadioButton.setFocusPainted(false);
                buttonGroup.add(jRadioButton);
                center.add(jRadioButton);
            }
        } catch (Exception e){
            JOptionPane.showMessageDialog(null, "数据库列表获取失败,错误信息：\n" + e.getMessage(), "提示", JOptionPane.WARNING_MESSAGE);
        }
    }
}
